package com.example.test_witns.ui.add_friend_requset;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.test_witns.R;
import com.example.test_witns.databinding.ActivityAddFriendRequestBinding;
import com.example.test_witns.ui.add_friends.AddFriendsActivity;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMFriendAddApplication;
import com.tencent.imsdk.v2.V2TIMFriendInfo;
import com.tencent.imsdk.v2.V2TIMFriendOperationResult;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;

public class ActivityAddFriendRequest extends AppCompatActivity {
    ActivityAddFriendRequestBinding binding;

    @Nullable
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAddFriendRequestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Objects.requireNonNull(getSupportActionBar()).hide();
        Window window = getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        window.setStatusBarColor(Color.parseColor("#eeeeee"));

        View add_friend_request_cancel = findViewById(R.id.add_friend_request_cancel);
        add_friend_request_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        CircleImageView add_request_avatar = findViewById(R.id.add_request_avatar);
        Bitmap bitmap = null;
        byte[] userAvatarBytes = null;
        String userAvatarURL = null;

        Intent intent = getIntent();
        userAvatarBytes = intent.getByteArrayExtra("userAvatarBytes");
        String uuid = intent.getStringExtra("uuid");
        String username = intent.getStringExtra("nickname");
        String userEmail = intent.getStringExtra("userEmail");
        userAvatarURL = intent.getStringExtra("userAvatarURL");
        if (userAvatarBytes != null){
            bitmap = BitmapFactory.decodeByteArray(userAvatarBytes, 0, userAvatarBytes.length);
            add_request_avatar.setImageBitmap(bitmap);
        }else {
            Glide.with(getApplicationContext())
                    .load(userAvatarURL)
                    .into(add_request_avatar);
        }
        TextView uuidTextView = findViewById(R.id.requested_uuid_value);
        uuidTextView.setText(uuid);

        TextView usernameTextView = findViewById(R.id.requested_username);
        usernameTextView.setText(username);

        View send_add_friend_request_confirm = findViewById(R.id.send_add_friend_request_confirm);
        send_add_friend_request_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userEmail != null){
                    V2TIMFriendAddApplication addApplication = new V2TIMFriendAddApplication(userEmail);
                    addApplication.setAddType(V2TIMFriendInfo.V2TIM_FRIEND_TYPE_BOTH);
                    V2TIMManager.getFriendshipManager().addFriend(addApplication, new V2TIMValueCallback<V2TIMFriendOperationResult>() {
                        @Override
                        public void onSuccess(V2TIMFriendOperationResult v2TIMFriendOperationResult) {
                            Toast.makeText(ActivityAddFriendRequest.this, "好友请求发送成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(ActivityAddFriendRequest.this, AddFriendsActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                            ActivityAddFriendRequest.this.startActivity(intent);
                            ActivityAddFriendRequest.this.finish();


                            V2TIMManager.getFriendshipManager().setFriendApplicationRead(new V2TIMCallback() {
                                @Override
                                public void onSuccess() {

                                }

                                @Override
                                public void onError(int i, String s) {

                                }
                            });
                        }

                        @Override
                        public void onError(int i, String s) {
                            Toast.makeText(ActivityAddFriendRequest.this, "出错了,请求失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });

    }
}
